var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var PositionGroup;
(function (PositionGroup) {
    PositionGroup[PositionGroup["t0"] = 0] = "t0";
    PositionGroup[PositionGroup["t1"] = 1] = "t1";
    PositionGroup[PositionGroup["t2"] = 2] = "t2";
    PositionGroup[PositionGroup["t3"] = 3] = "t3";
    PositionGroup[PositionGroup["t4"] = 4] = "t4";
    PositionGroup[PositionGroup["t5"] = 5] = "t5";
    PositionGroup[PositionGroup["t6"] = 6] = "t6";
    PositionGroup[PositionGroup["t7"] = 7] = "t7";
    PositionGroup[PositionGroup["t8"] = 8] = "t8";
    PositionGroup[PositionGroup["t9"] = 9] = "t9";
    PositionGroup[PositionGroup["d0"] = 10] = "d0";
    PositionGroup[PositionGroup["d1"] = 11] = "d1";
    PositionGroup[PositionGroup["d2"] = 12] = "d2";
    PositionGroup[PositionGroup["d3"] = 13] = "d3";
    PositionGroup[PositionGroup["d4"] = 14] = "d4";
    PositionGroup[PositionGroup["d5"] = 15] = "d5";
    PositionGroup[PositionGroup["d6"] = 16] = "d6";
    PositionGroup[PositionGroup["d7"] = 17] = "d7";
    PositionGroup[PositionGroup["d8"] = 18] = "d8";
    PositionGroup[PositionGroup["d9"] = 19] = "d9";
})(PositionGroup || (PositionGroup = {}));
var Cat = (function (_super) {
    __extends(Cat, _super);
    function Cat() {
        var _this = _super.call(this) || this;
        _this.addEventListener("added", _this.addedHandler);
        return _this;
    }
    Cat.prototype.addedHandler = function (e) {
        e.target.removeEventListener("added", e.target.addedHandler);
        e.target.initView();
    };
    Cat.prototype.initView = function () {
        var flameSheet = new lg.SpriteSheet({ images: ["../res/assets/images/cat.png"], frames: [[0, 0, 720, 887, 0, 360, 887], [0, 887, 720, 887, 0, 360, 887]] });
        this._skin = new lg.Sprite(flameSheet);
        this._skin.stop();
        this.addChild(this._skin);
    };
    Cat.prototype.initData = function () {
        this._presetPoints = new Array();
        var ppds = window["presetPoints"];
        for (var i = 0; i < ppds.length; i++) {
            var ppd = ppds[i];
            this._presetPoints.push({ point: new lg.Point(ppd.x, ppd.y), used: ppd.used, positionGroup: ppd.id });
        }
        this._skin.framerate = 0;
        this._skin.gotoAndStop(1);
    };
    Cat.prototype.getEnemyPoint = function () {
        var p = new lg.Point();
        var intervalW = 215 - (-125);
        var intervalH = 675 - 628;
        var rw = Math.random() * intervalW;
        var rh = Math.random() * intervalH;
        p.x = -125 + rw;
        p.y = -675 + rh;
        return this.localToGlobal(p.x, p.y);
    };
    Cat.prototype.getEnemyPresetPointDataByPreset = function () {
        var index = Math.floor(Math.random() * this._presetPoints.length);
        var ppd = this._presetPoints[index];
        if (ppd.used) {
            ppd = this.getEnemyPresetPointDataByPreset();
        }
        else {
            this._presetPoints[index].used = true;
        }
        return ppd;
    };
    Cat.prototype.getEnemyPresetPointDataByPositionGroup = function (v) {
        var ppd = this._presetPoints[v];
        if (ppd.used) {
            ppd = this.getEnemyPresetPointDataByPreset();
        }
        else {
            this._presetPoints[v].used = true;
        }
        return ppd;
    };
    Cat.prototype.getEnemyPointByPreset = function () {
        var p = new lg.Point();
        var ppd = this.getEnemyPresetPointDataByPreset();
        p = this.localToGlobal(ppd.point.x, ppd.point.y);
        return p;
    };
    return Cat;
}(lg.Container));
var Brush = (function (_super) {
    __extends(Brush, _super);
    function Brush() {
        var _this = _super.call(this) || this;
        _this._lastV = false;
        _this.addEventListener("added", _this.addedHandler);
        return _this;
    }
    Brush.prototype.addedHandler = function (e) {
        e.target.removeEventListener("added", e.target.addedHandler);
        e.target.initView();
    };
    Brush.prototype.startH = function () {
        this._tweenW.pause();
        this._tweenH.resume();
    };
    Brush.prototype.startW = function () {
        this._tweenH.pause();
        this._tweenW.resume();
    };
    Brush.prototype.stopTween = function () {
        this._tweenH.pause();
        this._tweenW.pause();
    };
    Brush.prototype.initView = function () {
        var _this = this;
        this._skinContainer = new lg.Container();
        this._normalSS = new lg.SpriteSheet({ images: ["../res/assets/images/brush_animation.png"], frames: [[0, 0, 270, 54, 0, 20, 25], [0, 54, 271, 55, 0, 20, 26], [0, 109, 270, 54, 0, 20, 25], [0, 54, 271, 55, 0, 20, 26]] });
        this._skin = new lg.Sprite(this._normalSS);
        this._skin.stop();
        this._skin.framerate = 6;
        this.addChild(this._skinContainer);
        this._skinContainer.addChild(this._skin);
        this._tweenH = TweenMax.to(this._skinContainer, 0.5, { x: -10, repeat: -1, yoyo: true, ease: Linear.easeNone, onComplete: function () {
            } });
        this._tweenW = TweenMax.to(this._skinContainer, 0.5, { y: -10, repeat: -1, yoyo: true, ease: Linear.easeNone, onComplete: function () {
            } });
        setInterval(function () {
            _this._lastV = !_this._lastV;
            _this.setMSkin(_this._lastV);
            _this._skin.play();
        });
        this.yoyo(false);
        this._mss = new lg.SpriteSheet({ images: ["../res/assets/images/brushM.png"], frames: [[0, 0, 272, 80, 0, 18, 10.05], [0, 0, 272, 80, 0, 18, 10.05]] });
    };
    Brush.prototype.setMSkin = function (v) {
        switch (App.lasetPositionGroup) {
            case PositionGroup.t0:
                this._skin.spriteSheet = this._normalSS;
                this.rotation = 0;
                this._skin.y = 18;
                this.startW();
                break;
            case PositionGroup.t1:
                this.startH();
                this._skin.spriteSheet = this._normalSS;
                this._skin.rotation = 90;
                this._skin.y = 0;
                this._skin.scaleX = 1;
                break;
            case PositionGroup.t2:
                this.startW();
                this._skin.spriteSheet = this._normalSS;
                this.rotation = 180;
                this._skin.y = -3;
                break;
            case PositionGroup.t3:
                this._skin.spriteSheet = this._normalSS;
                this.rotation = 180;
                this._skin.y = -2;
                break;
            case PositionGroup.t4:
                this._skin.spriteSheet = this._mss;
                this._skin.rotation = -85;
                this._skin.y = 0;
                this._skin.scaleX = 1;
                break;
            case PositionGroup.t5:
                this._skin.spriteSheet = this._mss;
                this.rotation = 70;
                this._skin.y = 4;
                this._skin.scaleX = -1;
                break;
            case PositionGroup.t6:
                this._skin.spriteSheet = this._mss;
                this.rotation = 0;
                this._skin.y = 4;
                this._skin.scaleX = -1;
                break;
            case PositionGroup.t7:
                this._skin.spriteSheet = this._mss;
                this.rotation = -25;
                this._skin.y = 2;
                this._skin.scaleX = -1;
                break;
            case PositionGroup.t8:
                this._skin.spriteSheet = this._mss;
                this.rotation = 80;
                this._skin.scaleX = 1;
                this._skin.y = 10;
                break;
            case PositionGroup.t9:
                this._skin.spriteSheet = this._mss;
                this._skin.rotation = -8;
                this._skin.y = 0;
                this._skin.scaleX = 1;
                break;
            case PositionGroup.d0:
                this._skin.spriteSheet = this._normalSS;
                this.rotation = 180;
                this._skin.y = -3;
                this._skin.scaleX = -1;
                break;
            case PositionGroup.d1:
                this._skin.spriteSheet = this._normalSS;
                this._skin.y = -3;
                this.rotation = 180;
                this._skin.scaleX = -1;
                break;
            case PositionGroup.d2:
                this._skin.spriteSheet = this._mss;
                this._skin.rotation = -90;
                this._skin.y = 0;
                this._skin.scaleX = -1;
                break;
            case PositionGroup.d3:
                this._skin.spriteSheet = this._mss;
                this._skin.y = 14;
                this._skin.scaleX = 1;
                this.rotation = 5;
                break;
            case PositionGroup.d4:
                this._skin.spriteSheet = this._mss;
                this._skin.scaleX = 1;
                this._skin.y = -3;
                this.rotation = 60;
                break;
            case PositionGroup.d5:
                this._skin.spriteSheet = this._mss;
                this._skin.rotation = 20;
                this._skin.y = -3;
                this._skin.scaleX = 1;
                break;
            default:
        }
    };
    Brush.prototype.yoyo = function (v) {
        if (this._t)
            v ? this._t.play() : this._t.pause();
    };
    return Brush;
}(lg.Container));
var UILayer = (function (_super) {
    __extends(UILayer, _super);
    function UILayer() {
        var _this = _super.call(this) || this;
        _this._progressBg = null;
        _this._progressBar = null;
        _this._btnmusic = null;
        _this._btnBack = null;
        _this.initSound();
        _this.addEventListener("added", _this.addedHandler);
        return _this;
    }
    UILayer.prototype.addedHandler = function (e) {
        e.target.removeEventListener("added", e.target.addedHandler);
        e.target.init();
    };
    UILayer.prototype.init = function () {
        var _this = this;
        this.y = 20;
        var canvas = this.stage.canvas;
        this._progressBg = new lg.Bitmap(App.loadManager.getImage("progress_bg"));
        this._progressBg.x = canvas.width - this._progressBg.getBounds().width >> 1;
        this._progressBg.y = 52;
        this.stage.addChild(this._progressBg);
        this._progressBar = new lg.Bitmap(App.loadManager.getImage("progress_bar"));
        this._progressBar.x = canvas.width - this._progressBar.getBounds().width >> 1;
        this._progressBar.y = 54;
        this.stage.addChild(this._progressBar);
        this._btnBack = new lg.Bitmap(App.loadManager.getImage("btnback"));
        this._btnBack.x = 40;
        this._btnBack.y = 40;
        this._btnBack.addEventListener("click", function () { window["backBtnHandler"](); }, null);
        this.stage.addChild(this._btnBack);
        this._btnmusic = new lg.Bitmap(App.loadManager.getImage("btnmusic"));
        this._btnmusic.x = canvas.width - 40 - this._btnmusic.getBounds().width;
        this._btnmusic.y = 40;
        this._btnmusic.addEventListener("click", function (e) { _this.musicHandler(e); }, null);
        this.stage.addChild(this._btnmusic);
        var time = new lg.Text("0分", "40px Arial", "#FF00FF");
        var totalTime = 180;
        var t = totalTime;
        time.text = t + "秒";
        time.textAlign = "center";
        time.x = canvas.width >> 1;
        time.y = 10;
        this.stage.addChild(time);
        setInterval(function () {
            _this._progressBar.scaleX = t / totalTime;
            t--;
            time.text = t + "秒";
            if (t == 0) {
                t = totalTime;
            }
        }, 1000);
    };
    UILayer.prototype.musicHandler = function (e) {
        window["musicHandler"]();
    };
    UILayer.eatSoundPlay = function () {
        window["eatSoundPlay"]();
    };
    UILayer.prototype.initSound = function () {
        var sounds = [
            { src: "../res/assets/audios/bg.mp3", id: "bg" },
            { src: "../res/assets/audios/eat.mp3", id: "eat" }
        ];
        lg.Sound.alternateExtensions = ["mp3"];
        lg.Sound.on("fileload", this.loadHandler, this);
        lg.Sound.registerSounds(sounds);
    };
    UILayer.prototype.loadHandler = function (event) {
        UILayer.bgSound = lg.Sound.play("bg");
        UILayer.bgSound.setLoop(-1);
        UILayer.eatSound = lg.Sound.createInstance("eat");
    };
    return UILayer;
}(lg.Container));
UILayer.musicPlaying = true;
var Enemy = (function (_super) {
    __extends(Enemy, _super);
    function Enemy(imageOrUrl) {
        return _super.call(this, imageOrUrl) || this;
    }
    Enemy.prototype.initData = function () {
        this._hp = 1;
        this._fight = false;
    };
    Enemy.prototype.start = function () {
        this._fight = true;
        this.loop();
    };
    Enemy.prototype.loop = function () {
    };
    Enemy.prototype.stop = function () {
        this._fight = false;
    };
    return Enemy;
}(lg.Bitmap));
var EventDispatcher = (function () {
    function EventDispatcher() {
        this._listeners = [];
        this.parent = null;
    }
    EventDispatcher.prototype.addEventListener = function (type, callback, scope, priority) {
        if (priority === void 0) { priority = 0; }
        var list = this._listeners[type];
        if (list == null) {
            this._listeners[type] = list = [];
        }
        var index = 0;
        var listener;
        var i = list.length;
        while (--i > -1) {
            listener = list[i];
            if (listener.c === callback) {
                list.splice(i, 1);
            }
            else if (index === 0 && listener.pr < priority) {
                index = i + 1;
            }
        }
        list.splice(index, 0, { c: callback, s: scope, pr: priority });
        return this;
    };
    EventDispatcher.prototype.removeEventListener = function (type, callback) {
        var list = this._listeners[type];
        if (list) {
            var i = list.length;
            while (--i > -1) {
                if (list[i].c === callback) {
                    list.splice(i, 1);
                    break;
                }
            }
        }
        return this;
    };
    EventDispatcher.prototype.dispatchEvent = function (type, data) {
        if (data === void 0) { data = null; }
        var evt = { type: type, target: this, data: data };
        var list = this._listeners[type];
        if (list) {
            var i = list.length;
            var listener;
            while (--i > -1) {
                listener = list[i];
                listener.c.call(listener.s, evt);
            }
        }
        if (this.parent) {
            this.parent.dispatchEvent(type, data);
        }
        return this;
    };
    return EventDispatcher;
}());
var BaseEvent = (function () {
    function BaseEvent(type, target, data) {
        if (data === void 0) { data = null; }
        this.type = null;
        this.target = null;
        this.data = null;
        this.type = type;
        this.target = target;
        this.data = data;
    }
    return BaseEvent;
}());
BaseEvent.CHANGE = "baseEventChange";
BaseEvent.COMPLETE = "baseEventComplete";
BaseEvent.ENTER_FRAME = "baseEventEnterFrame";
var LoaderEvent = (function (_super) {
    __extends(LoaderEvent, _super);
    function LoaderEvent(type, target) {
        return _super.call(this, type, target) || this;
    }
    return LoaderEvent;
}(BaseEvent));
LoaderEvent.COMPLETE = "loaderEventComplete";
LoaderEvent.LOAD_COMPLETE = "loaderEventLoadComplete";
var BulkLoader = (function (_super) {
    __extends(BulkLoader, _super);
    function BulkLoader() {
        var _this = _super.call(this) || this;
        _this._dataStores = [];
        _this.addEventListener(LoaderEvent.COMPLETE, _this.onLoadComplete, _this);
        return _this;
    }
    BulkLoader.getInstance = function () {
        if (BulkLoader._instance == null)
            BulkLoader._instance = new BulkLoader();
        return BulkLoader._instance;
    };
    BulkLoader.prototype.addFile = function (dataStore, key) {
        this._dataStores[key] = dataStore;
    };
    BulkLoader.prototype.getFile = function (key) {
        return this._dataStores[key];
    };
    BulkLoader.prototype.getImage = function (key) {
        console.log(key);
        return this._dataStores[key].data;
    };
    BulkLoader.prototype.load = function () {
        for (var key in this._dataStores) {
            var dataStore = this._dataStores[key];
            dataStore.addEventListener(LoaderEvent.COMPLETE, this.onLoadComplete, this);
            dataStore.load();
        }
    };
    BulkLoader.prototype.onLoadComplete = function (event) {
        event.target.removeEventListener(LoaderEvent.COMPLETE, this.onLoadComplete);
        for (var key in this._dataStores) {
            var dataStore = this._dataStores[key];
            if (!dataStore.complete) {
                return;
            }
        }
        this.dispatchEvent(LoaderEvent.LOAD_COMPLETE);
    };
    return BulkLoader;
}(EventDispatcher));
BulkLoader._instance = null;
var ImageLoader = (function (_super) {
    __extends(ImageLoader, _super);
    function ImageLoader(path) {
        var _this = _super.call(this) || this;
        _this._image = null;
        _this.complete = false;
        _this.src = path;
        var self = _this;
        _this._image = new Image();
        _this._image.onload = function () {
            self.onImageLoad();
        };
        return _this;
    }
    ImageLoader.prototype.load = function () {
        if (this.complete)
            return;
        this._image.src = this.src;
    };
    ImageLoader.prototype.onImageLoad = function () {
        this.complete = true;
        this.data = this._image;
        this.dispatchEvent(LoaderEvent.COMPLETE);
    };
    return ImageLoader;
}(EventDispatcher));
var App = (function () {
    function App() {
        var _this = this;
        this._resSuffix = "@2x";
        this.isDebug = true;
        this._coefficientNum = 0;
        this._finishCount = 1;
        this._enemysString = [
            "purple",
            "green"
        ];
        this.boomFlameSheet = new lg.SpriteSheet({ images: ["../res/assets/images/boom.png"], frames: [[0, 0, 70, 70, 0, 62.150000000000006, 20.549999999999997], [0, 0, 70, 70, 0, 62.150000000000006, 20.549999999999997], [70, 0, 105, 100, 0, 79.15, 32.55], [70, 0, 105, 100, 0, 79.15, 32.55], [0, 100, 141, 113, 0, 100.15, 43.55], [0, 100, 141, 113, 0, 100.15, 43.55], [0, 213, 166, 139, 0, 109.15, 51.55], [0, 213, 166, 139, 0, 109.15, 51.55], [0, 352, 91, 47, 0, 27.150000000000006, 47.55], [0, 352, 91, 47, 0, 27.150000000000006, 47.55]] });
        this.boom = new lg.Sprite(this.boomFlameSheet);
        lg.Ticker.addEventListener("tick", function () {
            if (_this.boomFlameSheet.getNumFrames(_this.boomFlameSheet.getAnimations()[0]) == _this.boom.currentFrame + 1) {
                _this.boom.gotoAndStop(1);
                _this.boom.visible = false;
            }
        });
        this._stage = new lg.Stage("canvasElement");
        App.loadManager = BulkLoader.getInstance();
        App.loadManager.addEventListener(LoaderEvent.LOAD_COMPLETE, this.handleComplete, this);
        var loadFiles = [
            { id: "background", src: "../res/assets/images/back-ground.png" },
            { id: "green", src: "../res/assets/images/left-up-green" + this._resSuffix + ".png" },
            { id: "purple", src: "../res/assets/images/left-down--purple" + this._resSuffix + ".png" },
            { id: "bubble", src: "../res/assets/images/bubble.png" },
            { id: "progress_bg", src: "../res/assets/images/progress_bg.png" },
            { id: "progress_bar", src: "../res/assets/images/progress_bar.png" },
            { id: "btnback", src: "../res/assets/images/btnback.png" },
            { id: "btnmusic", src: "../res/assets/images/btnmusic.png" },
            { id: "washbasin", src: "../res/assets/images/washbasin" + this._resSuffix + ".png" },
            { id: "toothDown", src: "../res/assets/images/tooth-down" + this._resSuffix + ".png" },
            { id: "toothUp", src: "../res/assets/images/tooth-up" + this._resSuffix + ".png" }
        ];
        for (var j = 0; j < loadFiles.length; j++) {
            var o = loadFiles[j];
            App.loadManager.addFile(new ImageLoader(o.src), o.id);
        }
        App.loadManager.load();
        this._scoreText = new lg.Text("0分", "40px Arial", "#FF00FF");
        lg.Ticker.addEventListener("tick", this._stage);
    }
    App.prototype.resetGame = function () {
        var _this = this;
        App.curScore = 0;
        this._cat._skin.gotoAndStop(0);
        this.initBrush();
        this._brush.visible = false;
        this._touchDown.visible = false;
        this._touchUp.visible = false;
        ;
        setTimeout(function () {
            _this.initBrush();
            _this._brush.visible = true;
        }, 500);
        setTimeout(function () { return _this.initData(); }, 2000);
        for (var i = 0; i < this._enemys.length; i++) {
            var e = this._enemys[i];
            e.parent.removeChild(e);
            this.boomFunc(e);
        }
        if (this._lastEnemy) {
            this._lastEnemy.parent.removeChild(this._lastEnemy);
            this._lastEnemy = null;
            this.boomFunc(this._lastEnemy);
        }
    };
    App.prototype.boomFunc = function (e) {
        console.log("e", e.x, e.y);
        var p = this._stage.localToGlobal(e.x, e.y);
        console.log("p", p.x, p.y);
        this.boom.x = p.x;
        this.boom.y = p.y;
        this.boom.play();
        this.boom.visible = true;
    };
    App.prototype.setScore = function () {
        App.curScore++;
        if (App.curScore == 100) {
            this.resetGame();
        }
        this.changeScore(App.curScore);
    };
    App.prototype.changeScore = function (v) {
        App.curScore = v;
        this._scoreText.text = v + "分";
    };
    App.prototype.handleComplete = function () {
        var _this = this;
        App.curScore = 0;
        this._stage.addChild(new lg.Bitmap(App.loadManager.getImage("background")));
        var canvas = this._stage.canvas;
        this._scoreText.x = canvas.width >> 1;
        this._scoreText.textAlign = "center";
        this._scoreText.y = 246;
        this._stage.addChild(this._scoreText);
        this._cat = new Cat();
        this._stage.addChild(this._cat);
        this._cat.y = canvas.height;
        this._cat.x = canvas.width >> 1;
        var washbasin = new lg.Bitmap(App.loadManager.getImage("washbasin"));
        this._stage.addChild(washbasin);
        washbasin.x = canvas.width - washbasin.image.width >> 1;
        washbasin.y = canvas.height - washbasin.image.height;
        this._brush = new Brush();
        this.initBrush();
        this._stage.addChild(this._brush);
        this.createBubbles();
        this._bubblesLayer.visible = false;
        setInterval(function () { return _this.loop(); }, 500);
        setTimeout(function () { return _this.initData(); }, 2000);
        this.touchInit();
        this._stage.addChild(new UILayer);
        this._stage.addChild(this.boom);
    };
    App.prototype.touchInit = function () {
        this._upToothLayer = new lg.Container();
        this._stage.addChild(this._upToothLayer);
        this._downToothLayer = new lg.Container();
        this._stage.addChild(this._downToothLayer);
        this._upToothLayer.name = "upToothLayer";
        this._downToothLayer.name = "downToothLayer";
        this._touchUp = new lg.Bitmap(App.loadManager.getImage("toothUp"));
        this._touchDown = new lg.Bitmap(App.loadManager.getImage("toothDown"));
        this._touchDown.x = 251;
        this._touchDown.y = 785;
        this._downToothLayer.addChild(this._touchDown);
        this._touchUp.x = 251;
        this._touchUp.y = 659;
        this._upToothLayer.addChild(this._touchUp);
        this._touchDown.visible = false;
        this._touchUp.visible = false;
        ;
    };
    App.prototype.initBrush = function () {
        this._brush.x = 165;
        this._brush.name = "brush";
        if (this._brush._skin) {
            this._brush._skin.gotoAndStop(1);
            this._brush.yoyo(false);
        }
        this._brush.scaleX = 1;
        this._brush.scaleY = 1;
        this._brush.y = 850;
        this._brush.rotation = 90;
    };
    App.prototype.initData = function () {
        var c = this._stage.canvas;
        this._cat.initData();
        this._coefficientNum = 0;
        this._finishCount = 1;
        this._brush.x = 135;
        this._brush.y = 1121;
        this._brush.scaleX = 1;
        this._brush.scaleY = 1;
        this._brush.rotation = -90;
        this._brush.visible = true;
        this._brush._skin.play();
        this._brush.yoyo(true);
        TweenMax.to(this._brush, 2, { scaleX: 0.8, scaleY: 0.8, rotation: 720 + this._lastBrushRotation });
        this._enemys = new Array();
        var data = new Array();
        var enemys = window["enemysData"];
        var d = this.getData();
        for (var i = 0; i < enemys.length; i++) {
            var ed = enemys[i];
            var enemy = this.createEmenyByID(ed.id);
            enemy.visible = false;
            var tempDisplay = true;
            if (d) {
                if (d[i].display == false) {
                    console.log(i, "已在上次被击杀");
                    tempDisplay = false;
                }
                else {
                    this.showEnemy(enemy, ed.time);
                }
            }
            else {
                this.showEnemy(enemy, ed.time);
            }
            var depths = window["depths"];
            var depth = depths[i];
            this.sortObjects(depth);
            if (depth.direction == "up") {
                if (depth.toothUp > depth.enmey) {
                    this._upToothLayer.addChildAt(enemy, this._upToothLayer.getNumChildren());
                }
                else {
                    this._upToothLayer.addChildAt(enemy, 0);
                }
            }
            else if (depth.direction == "down") {
                if (depth.toothDown > depth.enmey) {
                    this._downToothLayer.addChildAt(enemy, this._downToothLayer.getNumChildren());
                }
                else {
                    this._downToothLayer.addChildAt(enemy, 0);
                }
            }
            data.push({ index: i, display: tempDisplay });
        }
        this.savaData(data);
        this._coefficientNum = 100 / this._enemys.length;
        this._stage.addChild(this._brush);
        this._bubblesLayer.visible = true;
        this._touchDown.visible = true;
        this._touchUp.visible = true;
    };
    App.prototype.savaData = function (data) {
        var dj = JSON.stringify(data);
        localStorage.setItem("data", dj);
    };
    App.prototype.getData = function () {
        var dj;
        dj = localStorage.getItem("data");
        return dj ? JSON.parse(dj) : null;
    };
    App.prototype.showEnemy = function (enemy, time) {
        setTimeout(function () {
            enemy.visible = true;
        }, time);
    };
    App.prototype.nextEnemyFight = function (pos, isremove) {
        var data = this.getData();
        data[pos].display = !isremove;
        this.savaData(data);
        this._lastEnemy = this._enemys[pos];
        if (isremove) {
            this._lastEnemy.parent.removeChild(this._lastEnemy);
            this.boomFunc(this._lastEnemy);
            return;
        }
        this._lastEnemyPoint = new lg.Point(this._lastEnemy.x, this._lastEnemy.y);
        var pg = parseInt(this._lastEnemy.name);
        App.lasetPositionGroup = pg;
        this._lastBrushRotation = this._brush.rotation;
        var depths = window["depths"];
        var depth = depths[pg];
        this.sortObjects(depth);
        if (depth.direction == "up") {
            if (depth.toothUp < depth.enmey) {
                this._upToothLayer.addChildAt(this._brush, 0);
            }
            else {
                this._upToothLayer.addChildAt(this._brush, this._upToothLayer.getNumChildren());
            }
        }
        else if (depth.direction == "down") {
            if (depth.toothDown < depth.enmey) {
                this._downToothLayer.addChildAt(this._brush, 0);
            }
            else {
                this._downToothLayer.addChildAt(this._brush, this._downToothLayer.getNumChildren());
            }
        }
        this._lastEnemy.start();
        TweenMax.to(this._brush, 1, { x: this._lastEnemyPoint.x, y: this._lastEnemyPoint.y });
    };
    App.prototype.sortObjects = function (configData) {
        var sort = [
            this._stage.getChildIndex(this._upToothLayer),
            this._stage.getChildIndex(this._downToothLayer),
            this._stage.getChildIndex(this._brush)
        ];
        sort.sort(this.sortUpdate);
        var config = [
            { obj: this._brush, depth: configData.brush },
            { obj: this._downToothLayer, depth: configData.toothDown },
            { obj: this._upToothLayer, depth: configData.toothUp }
        ];
        config.sort(this.sortUpdateConfig);
        for (var i = 0; i < config.length; i++) {
            var obj = config[i].obj;
            this._stage.setChildIndex(obj, sort.shift());
        }
    };
    App.prototype.sortUpdateConfig = function (a, b) {
        return a.depth - b.depth;
    };
    App.prototype.sortUpdate = function (a, b) {
        return b - a;
    };
    App.prototype.tickHandler = function (e) {
        this.loop();
    };
    App.prototype.loop = function () {
        if (!this._lastEnemy)
            return;
        if (this._enemys.length == 0 && this._lastEnemy._hp == 0) {
            this.resetGame();
            return;
        }
        if (this._lastEnemy._hp == 0) {
            this._finishCount++;
        }
    };
    App.prototype.createEmeny = function () {
        var enemy = new Enemy(App.loadManager.getImage(this._enemysString[Math.floor(Math.random() * 1.9)]));
        enemy.scaleX = Math.random() < 0.5 ? 1 : -1;
        enemy.regX = enemy.getBounds().width >> 1;
        enemy.regY = enemy.getBounds().height >> 1;
        var scale = enemy.scaleX * 0.5;
        TweenMax.to(enemy, Math.random() * 2 + 1, { scaleX: scale, scaleY: 0.5, repeat: -1, yoyo: true });
        var p = new lg.Point();
        var ppd = this._cat.getEnemyPresetPointDataByPreset();
        p = this._cat.localToGlobal(ppd.point.x, ppd.point.y);
        enemy.x = p.x;
        enemy.y = p.y;
        enemy.name = ppd.positionGroup.toString();
        this._enemys.push(enemy);
    };
    App.prototype.createEmenyByID = function (v) {
        var enemy = new Enemy(App.loadManager.getImage(this._enemysString[Math.floor(Math.random() * 1.9)]));
        enemy.scaleX = Math.random() < 0.5 ? 1 : -1;
        enemy.regX = enemy.getBounds().width >> 1;
        enemy.regY = enemy.getBounds().height >> 1;
        var scale = enemy.scaleX * 0.5;
        TweenMax.to(enemy, Math.random() * 2 + 1, { scaleX: scale, scaleY: 0.5, repeat: -1, yoyo: true });
        var p = new lg.Point();
        var ppd = this._cat.getEnemyPresetPointDataByPositionGroup(v);
        p = this._cat.localToGlobal(ppd.point.x, ppd.point.y);
        enemy.x = p.x;
        enemy.y = p.y;
        enemy.name = ppd.positionGroup.toString();
        enemy.initData();
        this._enemys.push(enemy);
        return enemy;
    };
    App.prototype.createBubbles = function () {
        this._bubblesLayer = new lg.Container();
        this._stage.addChild(this._bubblesLayer);
        for (var i = 0; i < 10; i++) {
            var bubble = new lg.Bitmap(App.loadManager.getImage("bubble"));
            var c = this._stage.canvas;
            bubble.x = c.width * Math.random();
            bubble.y = c.height * Math.random();
            bubble.regX = bubble.getBounds().width >> 1;
            bubble.regY = bubble.getBounds().height >> 1;
            var s = Math.random() * 3;
            TweenMax.to(bubble, Math.random() * 5 + 5, { scaleX: s, scaleY: s, repeat: -1, alpha: Math.random(), rotation: Math.random() * 180, yoyo: true });
            this._bubblesLayer.addChild(bubble);
        }
    };
    return App;
}());
